#include "GUIPost3DVisualizationWidget.h"
#include "ui_GUIPost3DVisualizationWidget.h"

#include "FITK_Interface/FITKInterfaceCustomData/FITKCDInterface.h"
#include "FITK_Interface/FITKInterfaceCustomData/FITKCDPostprocess3DFixedWall.h"
#include "FITK_Interface/FITKInterfaceCustomData/FITKCDPostprocess3DRigidBody.h"
#include "FITK_Interface/FITKInterfaceCustomData/FITKCDPostprocess3DFLuid.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "OperatorsInterface/TreeEventOperator.h"

#include <QColorDialog>

using namespace GUI;

GUIPost3DVisualizationWidget::GUIPost3DVisualizationWidget(EventOper::ParaWidgetInterfaceOperator* oper, QWidget* parent) :
	GUIWidgetBase(parent), _oper(oper)
{
	_ui = new Ui::GUIPost3DVisualizationWidget();
	_ui->setupUi(this);

	init();
}

GUIPost3DVisualizationWidget::~GUIPost3DVisualizationWidget()
{
	if (_ui) {
		delete _ui;
		_ui = nullptr;
	}
}

void GUIPost3DVisualizationWidget::init()
{
	auto pFixedWallManager = Interface::FITKCDInterface::getInstance()->getCDPost3DFixedWallManager();
	_pFixedWall = pFixedWallManager->getDataByName("FixedWall");
	if (_pFixedWall == nullptr)
	{
		_pFixedWall = new Interface::FITKCDPost3DFixedWall();
		_pFixedWall->setDataObjectName("FixedWall");

		pFixedWallManager->appendDataObj(_pFixedWall);
	}

	auto pRigidBodyManager = Interface::FITKCDInterface::getInstance()->getCDPost3DRigidBodyManager();
	_pRigidBody = pRigidBodyManager->getDataByName("RigidBody");
	if (_pRigidBody == nullptr)
	{
		_pRigidBody = new Interface::FITKCDPost3DRigidBody();
		_pRigidBody->setDataObjectName("RigidBody");

		pRigidBodyManager->appendDataObj(_pRigidBody);
	}

	auto pFLuidManager = Interface::FITKCDInterface::getInstance()->getCDPost3DFLuidManager();
	_pFLuid = pFLuidManager->getDataByName("FLuid");
	if (_pFLuid == nullptr)
	{
		_pFLuid = new Interface::FITKCDPost3DFLuid();
		_pFLuid->setDataObjectName("FLuid");

		pFLuidManager->appendDataObj(_pFLuid);
	}

	_ui->comboBox_FW_Coloring->addItems(QStringList() << "Single Color");
	_ui->comboBox_RB_Coloring->addItems(QStringList() << "Single Color");
	_ui->comboBox_FL_Coloring->addItems(QStringList() << "Single Color");

	_ui->comboBox_FW_Display_Mode->addItems(QStringList() << "Mesh" << "Particle");
	_ui->comboBox_RB_Display_Mode->addItems(QStringList() << "Mesh" << "Particle");
	_ui->comboBox_FL_Display_Mode->addItems(QStringList() << "Mesh" << "Particle");

	setDataToWidget();
}

void GUIPost3DVisualizationWidget::setDataToWidget()
{
	_ui->comboBox_FW_Coloring->setCurrentIndex(_pFixedWall->getColoring());
	_ui->pushButton_FW_Single_Color->setStyleSheet(QString("QPushButton { background-color: %1; }").arg(_pFixedWall->getSingle_Color().name()));
	_ui->checkBox_FW_Visible->setChecked(_pFixedWall->getVisible());
	_ui->doubleSpinBox_FW_Opicity->setValue(_pFixedWall->getOpicity());
	_ui->comboBox_FW_Display_Mode->setCurrentIndex(_pFixedWall->getDisplay_Mode());

	_ui->comboBox_RB_Coloring->setCurrentIndex(_pRigidBody->getColoring());
	_ui->pushButton_RB_Single_Color->setStyleSheet(QString("QPushButton { background-color: %1; }").arg(_pRigidBody->getSingle_Color().name()));
	_ui->checkBox_RB_Visible->setChecked(_pRigidBody->getVisible());
	_ui->doubleSpinBox_RB_Opicity->setValue(_pRigidBody->getOpicity());
	_ui->comboBox_RB_Display_Mode->setCurrentIndex(_pRigidBody->getDisplay_Mode());

	_ui->comboBox_FL_Coloring->setCurrentIndex(_pFLuid->getColoring());
	_ui->pushButton_FL_Single_Color->setStyleSheet(QString("QPushButton { background-color: %1; }").arg(_pFLuid->getSingle_Color().name()));
	_ui->checkBox_FL_Visible->setChecked(_pFLuid->getVisible());
	_ui->doubleSpinBox_FL_Opicity->setValue(_pFLuid->getOpicity());
	_ui->comboBox_FL_Display_Mode->setCurrentIndex(_pFLuid->getDisplay_Mode());
}



void GUIPost3DVisualizationWidget::on_comboBox_FW_Coloring_activated(int index)
{
	_pFixedWall->setColoring(index);
}
void GUIPost3DVisualizationWidget::on_pushButton_FW_Single_Color_clicked()
{
	QColorDialog* pDlg = new QColorDialog(_pFixedWall->getSingle_Color());
	pDlg->setWindowFlags(Qt::MSWindowsFixedSizeDialogHint);
	if (pDlg->exec() == QDialog::Accepted)
	{
		QColor color = pDlg->selectedColor();
		_pFixedWall->setSingle_Color(color);

		_ui->pushButton_FW_Single_Color->setStyleSheet(QString("QPushButton { background-color: %1; }").arg(color.name()));
	}
}
void GUIPost3DVisualizationWidget::on_checkBox_FW_Visible_stateChanged(int state)
{
	_pFixedWall->setVisible(state);
}
void GUIPost3DVisualizationWidget::on_doubleSpinBox_FW_Opicity_valueChanged(double value)
{
	_pFixedWall->setOpicity(value);
}
void GUIPost3DVisualizationWidget::on_comboBox_FW_Display_Mode_activated(int index)
{
	_pFixedWall->setDisplay_Mode(index);
}

void GUIPost3DVisualizationWidget::on_comboBox_RB_Coloring_activated(int index)
{
	_pRigidBody->setColoring(index);
}
void GUIPost3DVisualizationWidget::on_pushButton_RB_Single_Color_clicked()
{
	QColorDialog* pDlg = new QColorDialog(_pRigidBody->getSingle_Color());
	pDlg->setWindowFlags(Qt::MSWindowsFixedSizeDialogHint);
	if (pDlg->exec() == QDialog::Accepted)
	{
		QColor color = pDlg->selectedColor();
		_pRigidBody->setSingle_Color(color);

		_ui->pushButton_RB_Single_Color->setStyleSheet(QString("QPushButton { background-color: %1; }").arg(color.name()));
	}
}
void GUIPost3DVisualizationWidget::on_checkBox_RB_Visible_stateChanged(int state)
{
	_pRigidBody->setVisible(state);
}
void GUIPost3DVisualizationWidget::on_doubleSpinBox_RB_Opicity_valueChanged(double value)
{
	_pRigidBody->setOpicity(value);
}
void GUIPost3DVisualizationWidget::on_comboBox_RB_Display_Mode_activated(int index)
{
	_pRigidBody->setDisplay_Mode(index);
}

void GUIPost3DVisualizationWidget::on_comboBox_FL_Coloring_activated(int index)
{
	_pFLuid->setColoring(index);
}
void GUIPost3DVisualizationWidget::on_pushButton_FL_Single_Color_clicked()
{
	QColorDialog* pDlg = new QColorDialog(_pFLuid->getSingle_Color());
	pDlg->setWindowFlags(Qt::MSWindowsFixedSizeDialogHint);
	if (pDlg->exec() == QDialog::Accepted)
	{
		QColor color = pDlg->selectedColor();
		_pFLuid->setSingle_Color(color);

		_ui->pushButton_FL_Single_Color->setStyleSheet(QString("QPushButton { background-color: %1; }").arg(color.name()));
	}
}
void GUIPost3DVisualizationWidget::on_checkBox_FL_Visible_stateChanged(int state)
{
	_pFLuid->setVisible(state);
}
void GUIPost3DVisualizationWidget::on_doubleSpinBox_FL_Opicity_valueChanged(double value)
{
	_pFLuid->setOpicity(value);
}
void GUIPost3DVisualizationWidget::on_comboBox_FL_Display_Mode_activated(int index)
{
	_pFLuid->setDisplay_Mode(index);
}
